package com.hp.controller;

import com.github.pagehelper.PageInfo;
import com.hp.entity.Good;
import com.hp.entity.Type;
import com.hp.service.IGoodService;
import com.hp.service.ITypeService;
import com.hp.util.UploadUtil;
import com.hp.vo.GoodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: LiuQiang
 * @Date: 2021/10/27 11:43
 */
@Controller
@RequestMapping("/admin")
public class GoodController {

    @Autowired
    private IGoodService goodService;
    @Autowired
    private ITypeService typeService;

    /**
     * 分页查询商品信息
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/goodList")
    public String goodList(@RequestParam("page") Integer page,
                           @RequestParam("type") Integer type, Model model){
        //返回一个goodVo全部信息(goods,typeName,top)
        PageInfo<GoodVo> pageInfo = goodService.findGoodAll(page,type);
        model.addAttribute("type",type);
        model.addAttribute("pageInfo",pageInfo);
        return "admins/good_list";
    }

    /**
     * 删除商品信息
     * @param goodId
     * @return
     */
    @GetMapping("/goodDelete")
    public String goodDelete(@RequestParam("id") Integer goodId){
        goodService.goodDelete(goodId);
        return "redirect:/admin/goodList?page=1&type=0";
    }

    /**
     * 移除今日推荐
     * @param goodId
     * @param type
     * @return
     */
    @PostMapping("/topDelete")
    @ResponseBody
    public String topDelete(@RequestParam("goodId") Integer goodId,
                            @RequestParam("type") Integer type){
        //根据good_id删除tops表信息
        int result = goodService.topDelete(goodId);
        if (result >= 1){
            return "ok";
        }else {
            return "no";
        }
    }

    /**
     * 加入今日推荐
     * @param goodId
     * @param type
     * @return
     */
    @PostMapping("/topSave")
    @ResponseBody
    public String topSave(@RequestParam("goodId") Integer goodId,
                          @RequestParam("type") Integer type){
        int result = goodService.topSave(goodId);
        if (result >= 1){
            return "ok";
        }else {
            return "no";
        }
    }

    /**
     * 跳转新增页面
     * @param model
     * @return
     */
    @GetMapping("/toGoodAdd")
    public String toGoodAdd(Model model){
        List<Type> typeList = typeService.findTypeAll();
        model.addAttribute("typeList",typeList);
        return "admins/good_add";
    }

    /**
     * 新增信息
     * @param good
     * @param file 图片上传
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/goodSave")
    public String goodSave(Good good, MultipartFile file, HttpServletRequest request) throws Exception {
        //图片处理
        good.setCover(UploadUtil.upload(file,request));
        //新增
        goodService.goodSave(good);
        return "redirect:/admin/goodList?page=1&type=0";
    }

    @GetMapping("/toGoodEdit")
    public String toGoodEdit(@RequestParam("id") Integer id,Model model){
        Good good = goodService.findGoodById(id);
        List<Type> typeList = typeService.findTypeAll();
        model.addAttribute("typeList",typeList);
        model.addAttribute("good",good);
        return "admins/good_edit";
    }

    @PostMapping("/goodUpdate")
    public String goodUpdate(Good good, MultipartFile file, HttpServletRequest request) throws Exception {
        if (null != file){
            good.setCover(UploadUtil.upload(file,request));
        }
        goodService.goodUpdate(good);
        return "redirect:/admin/goodList?page=1&type=0";
    }

}
